<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="utf-8"%>

<%@ include file="../include/head.jsp"%>
<%@ include file="../include/top.jsp"%>
<%@ include file="../include/left.jsp"%>

<div class="main-content">

	<%@ include file="../include/navigate.jsp"%>

	<div class="container">
		<c:if test="${error != null }">
			<div class="alert alert-danger alert-dismissable">
				<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
				<strong>异常:</strong> ${error}
			</div>
		</c:if>
		<c:if test="${msg != null }">
			<div class="alert alert-success alert-dismissable">
				<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
				<strong>提示:</strong> ${msg}
			</div>
		</c:if>
		<div class="widget-box">
			<div class="widget-box-header">
				<div class="col-sm-2">
					<div class="title">
						<i class="icon-edit">&nbsp;</i>更新数据源
					</div>
				</div>
			</div>
			<div class="widget-box-content" style="padding: 15px;">
				<form class="form-horizontal" role="form" action="${pageContext.request.contextPath}/connection/edit" method="post" onsubmit="return check();">
					<div class="form-group">
						<label class="col-sm-2 control-label">数据源名称</label>

						<div class="col-sm-5">
							<input name="connectionId" value="${connection.connectionId}" type="hidden" >
							<input type="text" class="form-control" name="connectionName" placeholder="数据源名称" value="${connection.connectionName}" oninput="validate(this)" onclick="validate(this)" required>
						</div>
						
						<div class="col-sm-2 control-label" id="connectionName_tips" style="text-align:left;color:#D16E6C;" hidden="true" >不能为空!</div>
					</div>

					<div class="form-group">
						<label class="col-sm-2 control-label">数据库类型</label>

						<div class="col-sm-5">
							<select class="form-control" name="databaseType" onChange="switchType(this)">
								<option value="DB2">DB2</option>
								<option value="SQL_Server">SQL Server</option>
								<option value="Oracle_SID">Oracle with SID</option>
								<option value="Oracle_Service_Name">Oracle with Service Name</option>
								<option value="Mongo">Mongo DB</option>
								<option value="Derby">Derby</option>
							</select>
						</div>
					</div>

					<div class="form-group">
						<label class="col-sm-2 control-label">主机ip</label>

						<div class="col-sm-5">
							<input type="text" class="form-control input-mask-hostip" name="hostIp" value="${connection.hostIp}" required>
						</div>
						
						<div class="col-sm-2 control-label" id="hostIp_tips" style="text-align:left;color:#D16E6C;" hidden="true" >请输入正确格式的ip!</div>
					</div>
					
					<div class="form-group">
						<label class="col-sm-2 control-label">端口</label>

						<div class="col-sm-5">
							<input type="text" class="form-control input-mask-port" name="port" value="${connection.port}" oninput="validate(this)" onclick="validate(this)" required>
						</div>
						
						<div class="col-sm-2 control-label" id="port_tips" style="text-align:left;color:#D16E6C;" hidden="true" >请输入正确的端口号!</div>
					</div>
					
					<div class="form-group">
						<label class="col-sm-2 control-label" id="databaseNameLabel">数据库名</label>

						<div class="col-sm-5">
							<input type="text" class="form-control" name="databaseName" value="${connection.databaseName}" placeholder="数据库名" oninput="validate(this)" onclick="validate(this)" required>
						</div>
						
						<div class="col-sm-2 control-label" id="databaseName_tips" style="text-align:left;color:#D16E6C;" hidden="true" >不能为空!</div>
					</div>
					
					<div class="form-group">
						<label class="col-sm-2 control-label">附加参数</label>

						<div class="col-sm-5">
							<input type="text" class="form-control" name="additionalParameter" value="${connection.additionalParameter}" placeholder="附加参数">
						</div>
					</div>
					
					<div class="form-group">
						<label class="col-sm-2 control-label">用户名</label>

						<div class="col-sm-5">
							<input type="text" class="form-control" name="username" placeholder="用户名" value="${connection.username}" oninput="validate(this)" onclick="validate(this)" required>
						</div>
						
						<div class="col-sm-2 control-label" id="username_tips" style="text-align:left;color:#D16E6C;" hidden="true" >不能为空!</div>
					</div>
					
					<div class="form-group">
						<label class="col-sm-2 control-label">密码</label>

						<div class="col-sm-5">
							<input type="text" class="form-control" name="password" placeholder="密码" value="${connection.password}" oninput="validate(this)" onclick="validate(this)" required>
						</div>
						
						<div class="col-sm-2 control-label" id="password_tips" style="text-align:left;color:#D16E6C;" hidden="true" >不能为空!</div>
					</div>
					
					<div class="form-group">
						<label class="col-sm-2 control-label"></label>

						<div class="col-sm-2">
							<button type="submit" class="btn btn-success">提交</button>
							<a type="button" class="btn btn-primary" href="${pageContext.request.contextPath}/connection/list">
								返回列表 
							</a>
						</div>
					</div>
				</form>
			</div>
		</div>
	</div>
	
	<div style="position: fixed; bottom: 0px; width: 100%;" align="center">
		<%@ include file="../include/footer.jsp"%>
	</div>
</div>

<script type="text/javascript">

	$(document).ready(function(){
		$("option").each(function(){
			if($(this).val() == '${connection.databaseType}'){
				$(this).attr("selected", "selected"); 
			}
		});
	});

	function switchType(selectOption){
		
		switch($(selectOption).val()){
			case "DB2":
				$("#schema_group").show();
				$("#databaseNameLabel").html("数据库名");
				$("input[name='port']").val("50000");
				break;
			case "SQL_Server":
				$("#schema_group").show();
				$("#databaseNameLabel").html("数据库名");
				$("input[name='port']").val("1433");
				break;
			case "Oracle_SID":
				$("#schema_group").show();
				$("#databaseNameLabel").html("SID");
				$("input[name='port']").val("1521");
				break;
			case "Oracle_Service_Name":
				$("#schema_group").show();
				$("#databaseNameLabel").html("服务名");
				$("input[name='port']").val("1521");
				break;
			case "Mongo":
				$("#schema_group").hide();
				$("#databaseNameLabel").html("数据库名");
				$("input[name='port']").val("27017");
				break;
			case "Derby":
				$("#schema_group").hide();
				$("#databaseNameLabel").html("数据库名");
				$("input[name='port']").val("1527");
				break;
			default:
				$("#schema_group").show();
				$("#databaseNameLabel").html("数据库名");
		}
	}
	
	$('.input-mask-hostip').inputmask('ip', {
		onincomplete : function() {
			$('#hostIp_tips').prop("hidden", false);
		},
		oncomplete : function() {
			$('#hostIp_tips').prop("hidden", true);
		},
		clearMaskOnLostFocus: false,
		insertMode: true
	});
	
	$('.input-mask-port').inputmask('9{1,}', {
		onincomplete : function() {
			$('#port_tips').prop("hidden", false);
		},
		oncomplete : function() {
			$('#port_tips').prop("hidden", true);
		},
		clearMaskOnLostFocus: false,
		insertMode: true
	});

	function validate(button){
		if($(button).val().length < 1){
			$(button).parent().next().show();
			return false;
		} else {
			$(button).parent().next().hide();
			return true;
		}
	}
	
	function check() {
		if(!validate($("input[name='connectionName']")) 
				|| !validate($("input[name='port']"))
				|| !validate($("input[name='databaseName']"))
				|| !validate($("input[name='username']"))
				|| !validate($("input[name='password']"))){
			return false;
		}
		
		if (!$('.input-mask-hostip').inputmask("isComplete")){
			$('#hostIp_tips').prop("hidden", false);
			return false;
		}
		
		if (!$('.input-mask-port').inputmask("isComplete")){
			$('#port_tips').prop("hidden", false);
			return false;
		}
		
		return true;
	}
</script>
	
<%@ include file="../include/bottom.jsp"%>